package com.deli.core.action;

import com.deli.core.pojo.Brand;
import com.deli.core.pojo.Color;
import com.deli.core.pojo.Product;
import com.deli.core.pojo.SuperPojo;
import com.deli.core.service.BrandService;
import com.deli.core.service.ColorService;
import com.deli.core.service.ProductService;
import com.deli.core.service.SolrService;
import com.deli.core.tools.Encoding;
import com.deli.core.tools.PageHelper.Page;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;


/**
 * 前台控制器
 * 
 * @author Administrator
 *
 */
@Controller
public class IndexAction {

	@Autowired
	private SolrService solrService;

	@Autowired
	private BrandService brandService;
	@Autowired
	private ProductService productService;
	@Autowired
	private ColorService colorService;

	@RequestMapping(value = "/")
	public String showPortal() {
		return "index";
	}

	@RequestMapping(value = "/search")
	public String toSearchKeyword(Model model, String keyword, String sort,
			Long brandId, Float pa, Float pb, Integer pageNum,
			Integer pageSize) throws SolrServerException {
		keyword = Encoding.encodeGetRequest(keyword);
		// 去solr索引库中去查询关键字
		if (sort == null || sort.isEmpty()) {
			sort = "price asc";
		}
		Page<SuperPojo> lsps = solrService.toSolrSearchKeyword(keyword, sort,brandId, pa, pb, pageNum, pageSize);
		// 去redis中查询品牌
        model.addAttribute("brandId",brandId);
		model.addAttribute("pa", pa);
		model.addAttribute("pb", pb);


        List<SuperPojo> result = lsps.getResult();
		List<Brand> brands = new ArrayList<Brand>();
		for (SuperPojo superPojo : result) {
			Brand brand = new Brand();
			Long brandId2 = (Long) superPojo.get("brandId");
			String name = brandService.findFormRedis(brandId2);
			brand.setId(brandId2);
			brand.setName(name);
			brands.add(brand);

		}
		//将
		Map<String, Object> map = new TreeMap<>();
		if(brandId!=null){
			for (Brand brand : brands) {
				if(brandId==brand.getId()){
					map.put("品牌",brand.getName());
					break;
				}
			}
		}
		if(pa!=null&&pb!=null){
			if(pb==-1){

				map.put("价格",pa+"以上");
			}else{
				map.put("价格",pa+"-"+pb);
			}
		}
		model.addAttribute("map",map);

		// List<Brand> brands = brandService.findFormRedis1();
		model.addAttribute("sort2", sort);
		if (sort.equals("price asc")) {
			sort = "price desc";
		} else {
			sort = "price asc";
		}
		model.addAttribute("listBrands", brands);
		model.addAttribute("sort", sort);
		model.addAttribute("lsps", lsps);
		model.addAttribute("keyword", keyword);
		return "search";
	}
	@RequestMapping(value = "/product/detail")
	public String toProductDetail(Model model,Long id){

		//查询数据库品牌数据
		Map<String,Object> productAndSkus = productService.findProductById(id);
		Product product = (Product) productAndSkus.get("product");
		System.out.println("product:大神告诉对方"+product.getName());
		List<Map<String,Object>> skus = (List<Map<String, Object>>) productAndSkus.get("skus");
		Map<Long, String> colors = new LinkedHashMap<Long, String>();
		for (Map<String, Object> sku : skus) {
			Long colorId = Long.parseLong(sku.get("color_id")+"");
			String colorName = (String) sku.get("colorName");
			colors.put(colorId,colorName);
		}
		productAndSkus.put("colors",colors);
		model.addAttribute("productAndSkus",productAndSkus);
		return "product";
	}
}
